package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.entity.Role;
import com.cskaoyan.bean.entity.Storage;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("admin/role")
public class RoleController {

    @Resource
    RoleService roleService;

    @GetMapping("options")
    public BaseRespVo options() {
        BasePageData data = roleService.options();

        return BaseRespVo.ok(data);
    }

    @RequestMapping("list")
    public BaseRespVo list(String name, BaseParam param) {
        BasePageData data = roleService.query(name, param);

        return BaseRespVo.ok(data);
    }

    /**
     * 添加 角色
     */
    @PostMapping("create")
    public BaseRespVo create(@RequestBody Map map) throws IOException {
        RoleCommonRespVo roleCommonRespVo = roleService.create(map);
        return BaseRespVo.ok(roleCommonRespVo);
    }
    /**
     * 修改 角色
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody Role role) {
        int i = roleService.update(role);
        if (i==0) return BaseRespVo.fail("修改失败");
        return BaseRespVo.ok();
    }
    /**
     * 删除 角色
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Storage storage) {
        int i = roleService.delete(storage.getId());
        if (i==0) return BaseRespVo.fail("当前角色存在管理员，不能删除");
        return BaseRespVo.ok();
    }
}